package com.ruoyi.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.edu.domain.vo.EduCourseVo;
import com.ruoyi.edu.mapper.EduCourseMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.edu.domain.bo.EduCourseDescriptionBo;
import com.ruoyi.edu.domain.vo.EduCourseDescriptionVo;
import com.ruoyi.edu.domain.EduCourseDescription;
import com.ruoyi.edu.mapper.EduCourseDescriptionMapper;
import com.ruoyi.edu.service.IEduCourseDescriptionService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 课程简介Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-22
 */
@RequiredArgsConstructor
@Service
public class EduCourseDescriptionServiceImpl implements IEduCourseDescriptionService {

    private final EduCourseDescriptionMapper baseMapper;
    private final EduCourseMapper eduCourseMapper;
    /**
     * 查询课程简介
     */
    @Override
    public EduCourseDescriptionVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询课程简介列表
     */
    @Override
    public TableDataInfo<EduCourseDescriptionVo> queryPageList(EduCourseDescriptionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EduCourseDescription> lqw = buildQueryWrapper(bo);
        Page<EduCourseDescriptionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<EduCourseDescriptionVo> records = result.getRecords();
        ArrayList<EduCourseDescriptionVo> list = new ArrayList<>();
        for (EduCourseDescriptionVo eduCourseDescriptionVo:records){
            EduCourseVo eduCourseVo = eduCourseMapper.selectVoById(eduCourseDescriptionVo.getId());
            eduCourseDescriptionVo.setTitle(eduCourseVo.getTitle());
            list.add(eduCourseDescriptionVo);
        }
        return TableDataInfo.build(list,result.getTotal());
    }

    /**
     * 查询课程简介列表
     */
    @Override
    public List<EduCourseDescriptionVo> queryList(EduCourseDescriptionBo bo) {
        LambdaQueryWrapper<EduCourseDescription> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EduCourseDescription> buildQueryWrapper(EduCourseDescriptionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EduCourseDescription> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDescription()), EduCourseDescription::getDescription, bo.getDescription());
        return lqw;
    }

    /**
     * 新增课程简介
     */
    @Override
    public Boolean insertByBo(EduCourseDescriptionBo bo) {
        EduCourseDescription add = BeanUtil.toBean(bo, EduCourseDescription.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改课程简介
     */
    @Override
    public Boolean updateByBo(EduCourseDescriptionBo bo) {
        EduCourseDescription update = BeanUtil.toBean(bo, EduCourseDescription.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EduCourseDescription entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除课程简介
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
